home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / Make / source / commands.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-27  |  13.0 KB  |  558 lines

  1. /* Command processing for GNU Make.
  2. Copyright (C) 1988,89,91,92,93,94,95,96,97 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "dep.h"
  21. #include "filedef.h"
  22. #include "variable.h"
  23. #include "job.h"
  24. #include "commands.h"
  25.  
  26. extern int remote_kill PARAMS ((int id, int sig));
  27.  
  28. #ifndef    HAVE_UNISTD_H
  29. extern int getpid ();
  30. #endif
  31.  
  32. /* Set FILE's automatic variables up.  */
  33.  
  34. static void
  35. set_file_variables (file)
  36.      register struct file *file;
  37. {
  38.   register char *p;
  39.   char *at, *percent, *star, *less;
  40.  
  41. #ifndef    NO_ARCHIVES
  42.   /* If the target is an archive member `lib(member)',
  43.      then $@ is `lib' and $% is `member'.  */
  44.  
  45.   if (ar_name (file->name))
  46.     {
  47.       unsigned int len;
  48.       p = index (file->name, '(');
  49.       at = (char *) alloca (p - file->name + 1);
  50.       bcopy (file->name, at, p - file->name);
  51.       at[p - file->name] = '\0';
  52.       len = strlen (p + 1);
  53.       percent = (char *) alloca (len);
  54.       bcopy (p + 1, percent, len - 1);
  55.       percent[len - 1] = '\0';
  56.     }
  57.   else
  58. #endif    /* NO_ARCHIVES.  */
  59.     {
  60.       at = file->name;
  61.       percent = "";
  62.     }
  63.  
  64.   /* $* is the stem from an implicit or static pattern rule.  */
  65.   if (file->stem == 0)
  66.     {
  67.       /* In Unix make, $* is set to the target name with
  68.      any suffix in the .SUFFIXES list stripped off for
  69.      explicit rules.  We store this in the `stem' member.  */
  70.       register struct dep *d;
  71.       char *name;
  72.       unsigned int len;
  73.  
  74. #ifndef    NO_ARCHIVES
  75.       if (ar_name (file->name))
  76.     {
  77.       name = index (file->name, '(') + 1;
  78.       len = strlen (name) - 1;
  79.     }
  80.       else
  81. #endif
  82.     {
  83.       name = file->name;
  84.       len = strlen (name);
  85.     }
  86.  
  87.       for (d = enter_file (".SUFFIXES")->deps; d != 0; d = d->next)
  88.     {
  89.       unsigned int slen = strlen (dep_name (d));
  90.       if (len > slen && !strncmp (dep_name (d), name + (len - slen), slen))
  91.         {
  92.           file->stem = savestring (name, len - slen);
  93.           break;
  94.         }
  95.     }
  96.       if (d == 0)
  97.     file->stem = "";
  98.     }
  99.   star = file->stem;
  100.  
  101.   /* $< is the first dependency.  */
  102.   less = file->deps != 0 ? dep_name (file->deps) : "";
  103.  
  104.   if (file->cmds == default_file->cmds)
  105.     /* This file got its commands from .DEFAULT.
  106.        In this case $< is the same as $@.  */
  107.     less = at;
  108.  
  109. #define    DEFINE_VARIABLE(name, len, value) \
  110.   (void) define_variable_for_file (name, len, value, o_automatic, 0, file)
  111.  
  112.   /* Define the variables.  */
  113.  
  114.   DEFINE_VARIABLE ("<", 1, less);
  115.   DEFINE_VARIABLE ("*", 1, star);
  116.   DEFINE_VARIABLE ("@", 1, at);
  117.   DEFINE_VARIABLE ("%", 1, percent);
  118.  
  119.   /* Compute the values for $^, $+, and $?.  */
  120.  
  121.   {
  122.     register unsigned int qmark_len, plus_len;
  123.     char *caret_value, *plus_value;
  124.     register char *cp;
  125.     char *qmark_value;
  126.     register char *qp;
  127.     register struct dep *d;
  128.     unsigned int len;
  129.  
  130.     /* Compute first the value for $+, which is supposed to contain
  131.        duplicate dependencies as they were listed in the makefile.  */
  132.  
  133.     plus_len = 0;
  134.     for (d = file->deps; d != 0; d = d->next)
  135.       plus_len += strlen (dep_name (d)) + 1;
  136.  
  137.     len = plus_len == 0 ? 1 : plus_len;
  138.     cp = plus_value = (char *) alloca (len);
  139.  
  140.     qmark_len = plus_len;    /* Will be this or less.  */
  141.     for (d = file->deps; d != 0; d = d->next)
  142.       {
  143.     char *c = dep_name (d);
  144.  
  145. #ifndef    NO_ARCHIVES
  146.     if (ar_name (c))
  147.       {
  148.         c = index (c, '(') + 1;
  149.         len = strlen (c) - 1;
  150.       }
  151.     else
  152. #endif
  153.       len = strlen (c);
  154.  
  155.     bcopy (c, cp, len);
  156.     cp += len;
  157. #if VMS
  158.         *cp++ = ',';
  159. #else
  160.     *cp++ = ' ';
  161. #endif
  162.     if (! d->changed)
  163.       qmark_len -= len + 1;    /* Don't space in $? for this one.  */
  164.       }
  165.  
  166.     /* Kill the last space and define the variable.  */
  167.  
  168.     cp[cp > plus_value ? -1 : 0] = '\0';
  169.     DEFINE_VARIABLE ("+", 1, plus_value);
  170.  
  171.     /* Make sure that no dependencies are repeated.  This does not
  172.        really matter for the purpose of updating targets, but it
  173.        might make some names be listed twice for $^ and $?.  */
  174.  
  175.     uniquize_deps (file->deps);
  176.  
  177.     /* Compute the values for $^ and $?.  */
  178.  
  179.     cp = caret_value = plus_value; /* Reuse the buffer; it's big enough.  */
  180.     len = qmark_len == 0 ? 1 : qmark_len;
  181.     qp = qmark_value = (char *) alloca (len);
  182.  
  183.     for (d = file->deps; d != 0; d = d->next)
  184.       {
  185.     char *c = dep_name (d);
  186.  
  187. #ifndef    NO_ARCHIVES
  188.     if (ar_name (c))
  189.       {
  190.         c = index (c, '(') + 1;
  191.         len = strlen (c) - 1;
  192.       }
  193.     else
  194. #endif
  195.       len = strlen (c);
  196.  
  197.     bcopy (c, cp, len);
  198.     cp += len;
  199. #if VMS
  200.     *cp++ = ',';
  201. #else
  202.     *cp++ = ' ';
  203. #endif
  204.     if (d->changed)
  205.       {
  206.         bcopy (c, qp, len);
  207.         qp += len;
  208. #if VMS
  209.         *qp++ = ',';
  210. #else
  211.         *qp++ = ' ';
  212. #endif
  213.       }
  214.       }
  215.  
  216.     /* Kill the last spaces and define the variables.  */
  217.  
  218.     cp[cp > caret_value ? -1 : 0] = '\0';
  219.     DEFINE_VARIABLE ("^", 1, caret_value);
  220.  
  221.     qp[qp > qmark_value ? -1 : 0] = '\0';
  222.     DEFINE_VARIABLE ("?", 1, qmark_value);
  223.   }
  224.  
  225. #undef    DEFINE_VARIABLE
  226. }
  227.  
  228. /* Chop CMDS up into individual command lines if necessary.
  229.    Also set the `lines_flag' and `any_recurse' members.  */
  230.  
  231. void
  232. chop_commands (cmds)
  233.      register struct commands *cmds;
  234. {
  235.   if (cmds != 0 && cmds->command_lines == 0)
  236.     {
  237.       /* Chop CMDS->commands up into lines in CMDS->command_lines.
  238.      Also set the corresponding CMDS->lines_flags elements,
  239.      and the CMDS->any_recurse flag.  */
  240.       register char *p;
  241.       unsigned int nlines, idx;
  242.       char **lines;
  243.  
  244.       nlines = 5;
  245.       lines = (char **) xmalloc (5 * sizeof (char *));
  246.       idx = 0;
  247.       p = cmds->commands;
  248.       while (*p != '\0')
  249.     {
  250.       char *end = p;
  251.     find_end:;
  252.       end = index (end, '\n');
  253.       if (end == 0)
  254.         end = p + strlen (p);
  255.       else if (end > p && end[-1] == '\\')
  256.         {
  257.           int backslash = 1;
  258.           register char *b;
  259.           for (b = end - 2; b >= p && *b == '\\'; --b)
  260.         backslash = !backslash;
  261.           if (backslash)
  262.         {
  263.           ++end;
  264.           goto find_end;
  265.         }
  266.         }
  267.  
  268.       if (idx == nlines)
  269.         {
  270.           nlines += 2;
  271.           lines = (char **) xrealloc ((char *) lines,
  272.                       nlines * sizeof (char *));
  273.         }
  274.       lines[idx++] = savestring (p, end - p);
  275.       p = end;
  276.       if (*p != '\0')
  277.         ++p;
  278.     }
  279.  
  280.       if (idx != nlines)
  281.     {
  282.       nlines = idx;
  283.       lines = (char **) xrealloc ((char *) lines,
  284.                       nlines * sizeof (char *));
  285.     }
  286.  
  287.       cmds->ncommand_lines = nlines;
  288.       cmds->command_lines = lines;
  289.  
  290.       cmds->any_recurse = 0;
  291.       cmds->lines_flags = (char *) xmalloc (nlines);
  292.       for (idx = 0; idx < nlines; ++idx)
  293.     {
  294.       int flags = 0;
  295.  
  296.       for (p = lines[idx];
  297.            isblank (*p) || *p == '-' || *p == '@' || *p == '+';
  298.            ++p)
  299.         switch (*p)
  300.           {
  301.           case '+':
  302.         flags |= COMMANDS_RECURSE;
  303.         break;
  304.           case '@':
  305.         flags |= COMMANDS_SILENT;
  306.         break;
  307.           case '-':
  308.         flags |= COMMANDS_NOERROR;
  309.         break;
  310.           }
  311.       if (!(flags & COMMANDS_RECURSE))
  312.         {
  313.           unsigned int len = strlen (p);
  314.           if (sindex (p, len, "$(MAKE)", 7) != 0
  315.           || sindex (p, len, "${MAKE}", 7) != 0)
  316.         flags |= COMMANDS_RECURSE;
  317.         }
  318.  
  319.       cmds->lines_flags[idx] = flags;
  320.       cmds->any_recurse |= flags & COMMANDS_RECURSE;
  321.     }
  322.     }
  323. }
  324.  
  325. /* Execute the commands to remake FILE.  If they are currently executing,
  326.    return or have already finished executing, just return.  Otherwise,
  327.    fork off a child process to run the first command line in the sequence.  */
  328.  
  329. void
  330. execute_file_commands (file)
  331.      struct file *file;
  332. {
  333.   register char *p;
  334.  
  335.   /* Don't go through all the preparations if
  336.      the commands are nothing but whitespace.  */
  337.  
  338.   for (p = file->cmds->commands; *p != '\0'; ++p)
  339.     if (!isspace (*p) && *p != '-' && *p != '@')
  340.       break;
  341.   if (*p == '\0')
  342.     {
  343.       /* We are all out of commands.
  344.      If we have gotten this far, all the previous commands
  345.      have run successfully, so we have winning update status.  */
  346.       file->update_status = 0;
  347.       notice_finished_file (file);
  348.       return;
  349.     }
  350.  
  351.   /* First set the automatic variables according to this file.  */
  352.  
  353.   initialize_file_variables (file);
  354.  
  355.   set_file_variables (file);
  356.  
  357.   /* Start the commands running.  */
  358.   new_job (file);
  359. }
  360.  
  361. /* This is set while we are inside fatal_error_signal,
  362.    so things can avoid nonreentrant operations.  */
  363.  
  364. int handling_fatal_signal = 0;
  365.  
  366. /* Handle fatal signals.  */
  367.  
  368. RETSIGTYPE
  369. fatal_error_signal (sig)
  370.      int sig;
  371. {
  372. #ifdef __MSDOS__
  373.   extern int dos_status, dos_command_running;
  374.  
  375.   if (dos_command_running)
  376.     {
  377.       /* That was the child who got the signal, not us.  */
  378.       dos_status |= (sig << 8);
  379.       return;
  380.     }
  381.   remove_intermediates (1);
  382.   exit (1);
  383. #else /* not __MSDOS__ */
  384. #ifdef _AMIGA
  385.   remove_intermediates (1);
  386.   if (sig == SIGINT)
  387.      fputs ("*** Break.\n", stderr);
  388.  
  389.   exit (10);
  390. #else /* not Amiga */
  391.   handling_fatal_signal = 1;
  392.  
  393.   /* Set the handling for this signal to the default.
  394.      It is blocked now while we run this handler.  */
  395.   signal (sig, SIG_DFL);
  396.  
  397.   /* A termination signal won't be sent to the entire
  398.      process group, but it means we want to kill the children.  */
  399.  
  400.   if (sig == SIGTERM)
  401.     {
  402.       register struct child *c;
  403.       for (c = children; c != 0; c = c->next)
  404.     if (!c->remote)
  405.       (void) kill (c->pid, SIGTERM);
  406.     }
  407.  
  408.   /* If we got a signal that means the user
  409.      wanted to kill make, remove pending targets.  */
  410.  
  411.   if (sig == SIGTERM || sig == SIGINT
  412. #ifdef SIGHUP
  413.     || sig == SIGHUP
  414. #endif
  415. #ifdef SIGQUIT
  416.     || sig == SIGQUIT
  417. #endif
  418.     )
  419.     {
  420.       register struct child *c;
  421.  
  422.       /* Remote children won't automatically get signals sent
  423.      to the process group, so we must send them.  */
  424.       for (c = children; c != 0; c = c->next)
  425.     if (c->remote)
  426.       (void) remote_kill (c->pid, sig);
  427.  
  428.       for (c = children; c != 0; c = c->next)
  429.     delete_child_targets (c);
  430.  
  431.       /* Clean up the children.  We don't just use the call below because
  432.      we don't want to print the "Waiting for children" message.  */
  433.       while (job_slots_used > 0)
  434.     reap_children (1, 0);
  435.     }
  436.   else
  437.     /* Wait for our children to die.  */
  438.     while (job_slots_used > 0)
  439.       reap_children (1, 1);
  440.  
  441.   /* Delete any non-precious intermediate files that were made.  */
  442.  
  443.   remove_intermediates (1);
  444.  
  445. #ifdef SIGQUIT
  446.   if (sig == SIGQUIT)
  447.     /* We don't want to send ourselves SIGQUIT, because it will
  448.        cause a core dump.  Just exit instead.  */
  449.     exit (EXIT_FAILURE);
  450. #endif
  451.  
  452.   /* Signal the same code; this time it will really be fatal.  The signal
  453.      will be unblocked when we return and arrive then to kill us.  */
  454.   if (kill (getpid (), sig) < 0)
  455.     pfatal_with_name ("kill");
  456. #endif /* not Amiga */
  457. #endif /* not __MSDOS__  */
  458. }
  459.  
  460. /* Delete FILE unless it's precious or not actually a file (phony),
  461.    and it has changed on disk since we last stat'd it.  */
  462.  
  463. static void
  464. delete_target (file, on_behalf_of)
  465.      struct file *file;
  466.      char *on_behalf_of;
  467. {
  468.   struct stat st;
  469.  
  470.   if (file->precious || file->phony)
  471.     return;
  472.  
  473. #ifndef NO_ARCHIVES
  474.   if (ar_name (file->name))
  475.     {
  476.       if (ar_member_date (file->name) != file->last_mtime)
  477.     {
  478.       if (on_behalf_of)
  479.         error ("*** [%s] Archive member `%s' may be bogus; not deleted",
  480.            on_behalf_of, file->name);
  481.       else
  482.         error ("*** Archive member `%s' may be bogus; not deleted",
  483.            file->name);
  484.     }
  485.       return;
  486.     }
  487. #endif
  488.  
  489.   if (stat (file->name, &st) == 0
  490.       && S_ISREG (st.st_mode)
  491.       && (time_t) st.st_mtime != file->last_mtime)
  492.     {
  493.       if (on_behalf_of)
  494.     error ("*** [%s] Deleting file `%s'", on_behalf_of, file->name);
  495.       else
  496.     error ("*** Deleting file `%s'", file->name);
  497.       if (unlink (file->name) < 0
  498.       && errno != ENOENT)    /* It disappeared; so what.  */
  499.     perror_with_name ("unlink: ", file->name);
  500.     }
  501. }
  502.  
  503.  
  504. /* Delete all non-precious targets of CHILD unless they were already deleted.
  505.    Set the flag in CHILD to say they've been deleted.  */
  506.  
  507. void
  508. delete_child_targets (child)
  509.      struct child *child;
  510. {
  511.   struct dep *d;
  512.  
  513.   if (child->deleted)
  514.     return;
  515.  
  516.   /* Delete the target file if it changed.  */
  517.   delete_target (child->file, (char *) 0);
  518.  
  519.   /* Also remove any non-precious targets listed in the `also_make' member.  */
  520.   for (d = child->file->also_make; d != 0; d = d->next)
  521.     delete_target (d->file, child->file->name);
  522.  
  523.   child->deleted = 1;
  524. }
  525.  
  526. /* Print out the commands in CMDS.  */
  527.  
  528. void
  529. print_commands (cmds)
  530.      register struct commands *cmds;
  531. {
  532.   register char *s;
  533.  
  534.   fputs ("#  commands to execute", stdout);
  535.  
  536.   if (cmds->filename == 0)
  537.     puts (" (built-in):");
  538.   else
  539.     printf (" (from `%s', line %u):\n", cmds->filename, cmds->lineno);
  540.  
  541.   s = cmds->commands;
  542.   while (*s != '\0')
  543.     {
  544.       char *end;
  545.  
  546.       while (isspace (*s))
  547.     ++s;
  548.  
  549.       end = index (s, '\n');
  550.       if (end == 0)
  551.     end = s + strlen (s);
  552.  
  553.       printf ("\t%.*s\n", (int) (end - s), s);
  554.  
  555.       s = end;
  556.     }
  557. }
  558.